"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.broadcastUnreadNotification = void 0;
const defines_1 = require("../../lib/defines");
const NoticeEntity_1 = require("../../httpServer/entity/NoticeEntity");
const log4js_1 = require("log4js");
const logger = (0, log4js_1.getLogger)('socket/adminUpdate');
function broadcastUnreadNotification(app) {
    const { socket } = app.locals;
    const { userId, username } = socket.handshake.query;
    socket.use(async (event, next) => {
        const [name, params] = event;
        if (name === defines_1.RequestMethod.login) {
            const notice = await NoticeEntity_1.NoticeEntity.findBy({ receiverId: userId, state: "UNREAD" });
            if (Array.isArray(notice) && notice.length) {
                for (const __notice of notice) {
                    let dbNotice = Object.assign(new NoticeEntity_1.NoticeEntity(), __notice, {
                        state: 'READ'
                    });
                    dbNotice = await dbNotice.save();
                    socket.emit('notice:message', dbNotice);
                }
            }
        }
        next();
    });
}
exports.broadcastUnreadNotification = broadcastUnreadNotification;
